package com.wavehi.service.impl;

import com.wavehi.mapper.IUserMapper;
import com.wavehi.pojo.User;
import com.wavehi.service.IUserService;
import com.wavehi.util.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @Author WaveHi
 * @Date 26/9/2021 下午 4:11
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description
 */
public class UserServiceImpl implements IUserService {
    @Override
    public Boolean addUser(User user) {
        SqlSession session = null;
        boolean isCompleted = false;
        try {
            //Get the session for database
            session = MyBatisUtils.getSession();
            //Get agent for interface 'IUserMapper'
            IUserMapper mapper = session.getMapper(IUserMapper.class);
            //Add user
            isCompleted = mapper.insert(user);
        } catch (Exception e) {
            e.printStackTrace();
            //Rollback the transaction.
            if(session != null) {
                session.rollback();
                session.close();
                session = null;
            }
        } finally {
            //Close resources
            if(session != null) {
                session.commit();
                session.close();
            }
            return isCompleted;
        }
    }

    @Override
    public Boolean deleteUser(Long id) {
        SqlSession session = null;
        boolean isCompleted = false;
        try {
            //Get the session for database
            session = MyBatisUtils.getSession();
            //Get agent for interface 'IUserMapper'
            IUserMapper mapper = session.getMapper(IUserMapper.class);
            //Delete user
            isCompleted = mapper.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            //Rollback transaction
            if(session != null) {
                session.rollback();
                session.close();
                session = null;
            }
        } finally {
            //Close resources
            if(session != null) {
                session.commit();
                session.close();
            }
            return isCompleted;
        }
    }

    @Override
    public Boolean updateUser(User user) {
        SqlSession session = null;
        boolean isCompleted = false;
        try {
            //Get the session for database
            session = MyBatisUtils.getSession();
            //Get the agent for interface 'IUserMapper'
            IUserMapper mapper = session.getMapper(IUserMapper.class);
            //Update user
            isCompleted = mapper.update(user);
        } catch (Exception e) {
            e.printStackTrace();
            //Rollback transaction
            if(session != null) {
                session.rollback();
                session.close();
                session = null;
            }
        } finally {
            //Close Resources
            if(session != null) {
                session.commit();
                session.close();
            }
            return isCompleted;
        }
    }

    @Override
    public Boolean userExists(String username) {
        SqlSession session = null;
        boolean exist = false;
        try {
            //Get the Session for database
            session = MyBatisUtils.getSession();
            //Get agent for interface 'IUserMapper'
            IUserMapper mapper = session.getMapper(IUserMapper.class);
            //Query data
            User user = mapper.selectByUsername(username);
            //Judge
            exist = user !=  null;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //Close resources
            if(session != null) {
                session.close();
            }
            return exist;
        }
    }

    @Override
    public Boolean userExists(String username, String password) {
        SqlSession session = null;
        boolean exist = false;
        try {
            //Get the session for database
            session = MyBatisUtils.getSession();
            //Get the agent for interface 'IUseMapper'
            IUserMapper mapper = session.getMapper(IUserMapper.class);
            //Query user
            User user = mapper.selectByUsernameAndPassword(username, password);
            //Judge result
            exist = user != null;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //Close resources
            if(session != null) {
                session.close();
            }
            return exist;
        }
    }

    @Override
    public User getUser(Long id) {
        SqlSession session = null;
        User user = null;
        try {
            //Get the session for database
            session = MyBatisUtils.getSession();
            //Get the agent for interface 'IUserMapper'
            IUserMapper mapper = session.getMapper(IUserMapper.class);
            //Query user
            user = mapper.selectById(id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //Close resources
            if(session != null) {
                session.close();
            }
            return user;
        }
    }

    @Override
    public User getUser(String username) {
        SqlSession session = null;
        User user = null;
        try {
            //Get the session for database
            session = MyBatisUtils.getSession();
            //Get agent session for interface 'IUserMapper'
            IUserMapper mapper = session.getMapper(IUserMapper.class);
            //Query user
            user = mapper.selectByUsername(username);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //Close resources
            if(session != null) {
                session.close();
            }
            return user;
        }
    }

    @Override
    public List<User> getAllUsers() {
        SqlSession session = null;
        List<User> users = null;
        try {
            //Get the session for database
            session = MyBatisUtils.getSession();
            //Get the agent for interface 'IUserMapper'
            IUserMapper mapper = session.getMapper(IUserMapper.class);
            //Query users
            users = mapper.selectAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //Close resources
            if(session != null) {
                session.close();
            }
            return users;
        }
    }
}
